Skip to content

Conversation

@devunwired
Copy link

Update the platform logging to use the ESP_LOGx macros when MO_PLATFORM is set to MO_PLATFORM_ESPIDF.

This allows log messages from the library to be managed properly alongside other application logs when using functions from the ESP-IDF logging library like esp_log_level_set() to supress logs or esp_log_set_vprintf() to route them to a different destination.

Update the platform logging to use the ESP_LOGx macros when MO_PLATFORM
is set to MO_PLATFORM_ESPIDF.
@devunwired
Copy link
Author

devunwired commented Mar 7, 2025

Looking into this a bit more deeply, this isn't a great drop-in replacement because ESP_LOGx assumes each call is a full "log message" and the macros in Debug.h assume they can piecemeal the log entry parts using printf semantics. Replacing them in this way generates 3 lines for every log (header, message, newline).

I also didn't previously realize that log levels were actually supported in Debug.h they just aren't reflected out to Platform.h. The best direct replacement here is esp_log_write(), but we still lose the level data.

@matth-x do you have any suggestions on the best way to modify the logging infrastructure here to cleanly insert ESP_LOGx with support for levels in the platform layer?

@razvanphp
Copy link
Contributor

We would also love to have this, as we then tunnel ESP-IDF logs into our web interface.

Any way we can support to make this happen?

@devunwired
Copy link
Author

Thanks @razvanphp, we do the same. Our current workaround is to use MO_CUSTOM_CONSOLE and mocpp_set_console_out() to provide a custom logging function that uses esp_log_write(), but there are still gaps. Mainly being able to pass forward the log level.

Either way, I may patch this to use esp_log_write instead since it's a better replacement for printf, but we would still need a maintainer to review and approve.

@matth-x
Copy link
Owner

matth-x commented Dec 2, 2025

@devunwired Thanks for proposing a better solution to this!

In the current debug macros, the log levels handling is a pain and it will be improved in the next MO major version. The current possible workarounds are:

  • Forward everything as "info" message to the ESP logger library
  • Do a string match and if the log message starts with [MO] info, then forward the following as info message

Neither of the workarounds are great. I would tend to leave it to the implementer to choose the workaround and make the log messages work on their platform. And otherwise, encourage the MOv2 upgrade. Do you agree, or do you think that the esp_log_write() function is a clear benefit for the general MO user?

In MOv2, it will be possible to set the debugCb2() which also passes the log level to the callback:

void (*debugCb2)(int lvl, const char *fn, int line, const char *msg) = nullptr;

I see that the built-in log function makes the same mistake as in the current version and doesn't make use of the ESP logger library. Noted on my end!

Replace the ESP_LOG macro with esp_log_write, which is a better
replacement for printf style logging.
@devunwired
Copy link
Author

devunwired commented Dec 3, 2025

I just bumped this change to use esp_log_write(), but if this is handled differently in 2.0 then it may just be informational.

I would tend to leave it to the implementer to choose the workaround and make the log messages work on their platform. And otherwise, encourage the MOv2 upgrade. Do you agree, or do you think that the esp_log_write() function is a clear benefit for the general MO user?

IMO, the callback mechanism makes sense for implementers using a platform that doesn't have built-in support or if you have custom processing to do on the log messages.

Since the library has a platform layer with built-in support for ESP-IDF, then esp_log_write() is the proper implementation of printf() for that platform; and it aligns with the way MicroOcpp is using log functions (multiple calls per line to build a message).

Note: There is also a esp_log_writev(), in case that integrates better with the logging framework.

@matth-x matth-x merged commit ee1271b into matth-x:main Dec 4, 2025
9 of 10 checks passed
@matth-x
Copy link
Owner

matth-x commented Dec 4, 2025

Okay, agreed. Thanks for your improvement to MicroOCPP, Dave!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants